import { NFont } from '../types';
import { getPixelRatio } from './tools';
type Toption = {

}
type Tstyle = {
    fontSize: number;
    position: {
        x: number;
        y: number;
        w: number;
        h: number;
    },
    vertical: NFont.EVertical,
    horizontally: NFont.EHorizontally
    wrap: 'auto'|'no';
    color: string;
    fontFamily: string;
    weight: number;
    style: NFont.EFontStyle
}

export function textRender(ctx: CanvasRenderingContext2D, text: string, style: Tstyle, pixelRatio: number) {
    const fontSize = style.fontSize * pixelRatio;
    const { position } = style;
    let fontW = ctx.measureText(text).width;
    // italic 斜体 oblique 斜体
    let fontStyle = 'normal';
    if(style.style === NFont.EFontStyle.italic) {
        fontStyle = 'italic';
    } else if(style.style === NFont.EFontStyle.oblique) {
        fontStyle = 'oblique';
    }
    ctx.font = `${fontStyle} normal ${style.weight === 900 ? 'bold': 'normal'} ${fontSize}px Arial`;
    // ctx.font = `${fontSize}px "${style.fontFamily}" ${style.weight === 900 ? 'bold': 'normal'}`;
    let x = style.position.x, y = style.position.y + style.position.h / 2;
    ctx.textBaseline = 'middle';
    if(style.color) {
        ctx.fillStyle =  style.color;
    }
    if(style.wrap === 'no') {
        if(style.vertical === NFont.EVertical.middle) {
            ctx.textBaseline = 'middle';
            x = style.position.x, y = style.position.y + style.position.h / 2;
        }
        if(fontW < position.w) {
            if(style.horizontally === NFont.EHorizontally.center) {
                x = (position.w - fontW)/2 + x;
            }
            if(style.horizontally === NFont.EHorizontally.right) {
                x = x + (position.w - fontW);
            }
        }
        if(fontSize < position.h) {
            if(style.vertical === NFont.EVertical.top) {
                ctx.textBaseline = 'top';
                y = position.y;
            }
            if(style.vertical === NFont.EVertical.bottom) {
                ctx.textBaseline = 'bottom';
                y = position.y + position.h;
            }
        }
        ctx.fillText(text, x, y);
    } else {
        
    }
}